class Solution {
public:
    bool canPartition(vector<int>& nums)
    {
        int m = nums.size();
        int sum = 0;
        for (auto& e : nums) sum += e;
        if (sum % 2 == 1) return false;
        int n = sum / 2;
        vector<bool> dp(n + 1);
        dp[0] = true;
        for (int i = 1; i <= m; i++)
        {
            for (int j = n; j >= nums[i - 1]; j--)
            {
                dp[j] = dp[j] || dp[j - nums[i - 1]];
            }
        }
        return dp[n];
    }
};
class Solution {
public:
    int maxProduct(vector<int>& nums)
    {
        int n = nums.size();
        vector<int> f(n), g(n);
        f[0] = g[0] = nums[0];
        int ret = nums[0];
        for (int i = 1; i < n; i++)
        {
            f[i] = max(nums[i], max(f[i - 1] * nums[i], g[i - 1] * nums[i]));
            g[i] = min(nums[i], min(f[i - 1] * nums[i], g[i - 1] * nums[i]));
            ret = max(ret, f[i]);
        }
        return ret;
    }
};
class Solution {
public:
    int lengthOfLIS(vector<int>& nums)
    {
        int n = nums.size();
        vector<int> dp(n, 1);
        int ret = 1;
        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j < i; j++)
            {
                if (nums[i] > nums[j])
                {
                    dp[i] = max(dp[i], dp[j] + 1);
                }
            }
            ret = max(ret, dp[i]);
        }
        return ret;
    }
};
class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict)
    {
        unordered_set<string> hash;
        for (auto& e : wordDict) hash.insert(e);

        int n = s.size();
        vector<bool> dp(n + 1);
        dp[0] = true;
        s = ' ' + s;
        for (int i = 1; i <= n; i++)
        {
            for (int j = i; j >= 0; j--)
            {
                if (hash.count(s.substr(j, i - j + 1)) && dp[j - 1] == true)
                {
                    dp[i] = true;
                }
            }
        }
        return dp[n];
    }
};
class Solution {
public:
    int coinChange(vector<int>& coins, int amount)
    {
        const int INF = 0x3f3f3f3f;
        int m = coins.size(), n = amount;
        vector<int> dp(n + 1);
        for (int j = 1; j <= n; j++) dp[j] = INF;

        for (int i = 1; i <= m; i++)
        {
            for (int j = 0; j <= n; j++)
            {
                if (j >= coins[i - 1]) dp[j] = min(dp[j], dp[j - coins[i - 1]] + 1);
            }
        }
        return dp[n] == INF ? -1 : dp[n];
    }
};